﻿using System;
using System.Data;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Openfree.Framework.DataAccess;

public partial class PMSWeb_CostItem : PMSBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.TableName = "PMS_CostItem";
        this.Key = "CostID";
        if (!IsPostBack)
        {
            pnlGrid.Visible = true;
            pnlEdit.Visible = false;
            string cmdText = "select * from PMS_CostItem order by OrderIndex";

            FillGirdView(0, cmdText);
        }
        else
        {
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
        }
    }
   
    protected void FillGirdView(int currentPage, string cmdText)
    {
        if (string.IsNullOrEmpty(cmdText))
        {
            cmdText = ViewState[this.ClientID + "cmdText"].ToString();
        }
        SqlSplitPage splitPage = new SqlSplitPage(base.Connection, cmdText);
        splitPage.PageSize = 10;
        DataSet ds = splitPage.LoadDataSet(currentPage);
        //成功查询出数据后缓存
        ViewState[this.ClientID + "cmdText"] = cmdText;
        gridView.ShowFooter = true;
        gridView.NavigationData = splitPage.GetNavigationData(currentPage);
        gridView.DataSource = ds.Tables[0];
        gridView.DataBind();
    }

    protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        { 
            e.Row.Cells[5].Text = FormatBoolean(e.Row.Cells[5].Text, "是", "否");
            e.Row.Cells[6].Text = FormatBoolean(e.Row.Cells[6].Text, "是", "否");
            e.Row.Cells[7].Text = FormatBoolean(e.Row.Cells[7].Text, "是", "否");
        }
    }

    protected void gridView_SplitPageIndexChange(object sender, CommandEventArgs e)
    {
        FillGirdView(Int32.Parse(e.CommandArgument.ToString()), ViewState[this.ClientID + "cmdText"].ToString());
    }

    protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        hfCostID.Value = e.CommandArgument.ToString();
        EditDate();
    }

    private void ResetComponent()
    {
        tbCycle.Text = "";
        tbDescription.Text = "";
        tbMoney.Text = "";
        tbSubject.Text = "";
        tbOrderIndex.Text = "";
    }
 
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        hfAddOrEdit.Value = "Add";
        hfCostID.Value = string.Empty;
        pnlGrid.Visible = false;
        pnlEdit.Visible = true;
        ResetComponent(); 
    }

    protected void btnRemove_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Request["CostID"]))
        {
            base.Alert("请选择一条记录");
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
            return;
        }
        PMSAccess.RemoveDataRow(this.TableName, this.Key, Request["CostID"].ToString());

        FillGirdView(this.gridView.SplitPageIndex, string.Empty);
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Request["CostID"]))
        {
            base.Alert("请选择一条记录");
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
            return;
        }
        hfCostID.Value = Request["CostID"];
        EditDate();
    }

    private void EditDate()
    {
        ResetComponent();
        hfAddOrEdit.Value = "Edit";
        pnlGrid.Visible = false;
        pnlEdit.Visible = true;
        DataSet ds = PMSAccess.GetDataSet(this.TableName, this.Key, hfCostID.Value);
        DataRow row = ds.Tables[0].Rows[0];

        tbCycle.Text = row["Cycle"].ToString();
        tbDescription.Text = row["Description"].ToString();
        tbMoney.Text = row["Money"].ToString();
        tbSubject.Text = row["Subject"].ToString();
        tbOrderIndex.Text = row["OrderIndex"].ToString();
        rbIsDeleted.SelectByValue = row["IsDeleted"].ToString();
        rbIsSelected.SelectByValue = row["IsSelected"].ToString();
        rbIsUsed.SelectByValue = row["IsUsed"].ToString();
    }


    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnlGrid.Visible = true;
        pnlEdit.Visible = false;
        FillGirdView(this.gridView.SplitPageIndex, string.Empty);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (hfAddOrEdit.Value == "Add")
        {
            ExecuteAdd(); 
        }
        else
        {
            ExecuteEdit();
            btnCancel_Click(sender, e);
        }
    }

    private void ExecuteAdd()
    {
        string costID = Openfree.Framework.Common.Convert.ChineseToPYAbbreviation(tbSubject.Text.Trim()).ToUpper();
        string cmdText = "select CostID from PMS_CostItem where CostID='{0}'";
        cmdText = string.Format(cmdText, costID);
        string result = BaseAccess.ExecuteScalar(base.Connection, cmdText);
        if(result != string.Empty)
        {
            base.Alert("保存失败，主键冲突["+ costID +"]");
            return;
        }

        DataSet ds = PMSAccess.GetEmptyDataSet(this.TableName);
        DataRow row = ds.Tables[0].NewRow();
        row["CostID"] = costID;
        row["Cycle"] = tbCycle.Text.Trim();
        row["Description"] = tbDescription.Text.Trim();
        row["Money"] = tbMoney.Text.Trim() == string.Empty ? 0 : float.Parse(tbMoney.Text.Trim());
        row["Subject"] = tbSubject.Text.Trim();
        row["OrderIndex"] = tbOrderIndex.Text.Trim();
        row["IsDeleted"] = rbIsDeleted.SelectedValue;
        row["IsSelected"] = rbIsSelected.SelectedValue;
        row["IsUsed"] = rbIsUsed.SelectedValue;

        ds.Tables[0].Rows.Add(row);
        PMSAccess.UpdateDataSet(this.TableName, ds.GetChanges());

        ResetComponent();
        base.Alert("新建成功");
    }


    private void ExecuteEdit()
    {
        DataSet ds = PMSAccess.GetDataSet(this.TableName, this.Key, hfCostID.Value);
        DataRow row = ds.Tables[0].Rows[0];
        row["Cycle"] = tbCycle.Text;
        row["Description"] = tbDescription.Text;
        row["Money"] = tbMoney.Text.Trim() == string.Empty ? 0 : float.Parse(tbMoney.Text.Trim());
        row["Subject"] = tbSubject.Text;
        row["OrderIndex"] = tbOrderIndex.Text;
        row["IsDeleted"] = rbIsDeleted.SelectedValue;
        row["IsSelected"] = rbIsSelected.SelectedValue;
        row["IsUsed"] = rbIsUsed.SelectedValue;
        PMSAccess.UpdateDataSet(this.TableName, ds.GetChanges());
    }
}
